/*
 * Copyright @ 2015 Goldpac Co. Ltd. All right reserved.
 * 
 * @fileName DESUtils.java
 * 
 * @author rui.kang
 */
package com.goldpac.cardbiz.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.codec.binary.Base64;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.errors.IntrusionException;
import org.owasp.esapi.errors.ValidationException;

import com.goldpac.test.ReturnPath;

import sun.misc.BASE64Encoder;

public class Base64Utils {

    /**
     * 对字符串进行加密，返回BASE64的加密字符串 <功能详细描述>
     * 
     * @param str
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getEncryptString(String str) {
        // BASE64Encoder base64Encoder = new BASE64Encoder();
        try {
            byte[] strBytes = str.getBytes("UTF-8");
            return Base64.encodeBase64String(strBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对BASE64加密字符串进行解密 <功能详细描述>
     * 
     * @param str
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getDecryptString(String str) {
        // BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            byte[] strBytes = Base64.decodeBase64(str);
            return new String(strBytes, "GBK");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将string类型的数据转码为byte类型.
     * 
     * @param fileData String 类型的数据.
     * @return 转码后的数据byte类型,发生异常或者filedate为null时返回null.
     */
    public static byte[] decode(String fileData) {

        if (fileData == null) {
            return null;
        }
        return Base64.decodeBase64(fileData);
    }

    public static String fileToBase64(String fileName) throws IOException, IntrusionException, ValidationException {
        // filename; // 源文件
        String strBase64 = null;
        InputStream in = null;
        // String binFileName = validFilePath(input + "\\" + cardNo + ".bin", input, input);
        String name = ReturnPath.getPath(fileName);
        try {
            in = new FileInputStream(name);
            // in.available()返回文件的字节长度
            byte[] bytes = new byte[in.available()];
            // 将文件中的内容读入到数组中
            in.read(bytes);
            // strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
            strBase64 = encode(bytes); // 将字节流数组转换为字符串
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (in != null)
                in.close();
        }
        return strBase64;
    }

    /**
     * 编码
     * 
     * @param bstr
     * @return String
     */
    public static String encode(byte[] bstr) {
        return new BASE64Encoder().encode(bstr);
    }

    public static String validFilePath(String fileName, String inputPath, String rootPath) throws ValidationException {
        return ESAPI.validator().getValidDirectoryPath(fileName, inputPath, new File(rootPath), false);
    }

}
